class Solution
{
public:
    vector<int> maxAltitude(vector<int>& heights, int limit)
    {
        vector<int> ans;
        deque<int> q;
        for (int i = 0; i < heights.size(); i++)
        {
            while (!q.empty() && heights[q.back()] <= heights[i])
                q.pop_back();

            q.push_back(i);

            if (i - q.front() >= limit)
                q.pop_front();

            if (i >= limit - 1)
                ans.push_back(heights[q.front()]);
        }
        return ans;
    }
};